package cn.ffcs.bpmpvc.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.quartz.CronTriggerBean;

import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.bpmpvc.graph.FlowPvcGraphService;
import cn.ffcs.bpmpvc.graph.Node;
import cn.ffcs.bpmpvc.services.AppBusinessReqService;
import cn.ffcs.bpmpvc.svc.ProcessPvcService;
import cn.ffcs.bpmpvc.svc.TaskAssignBean;
import cn.ffcs.bpmpvc.svc.TaskService;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.contract.service.ContractRemindService;
import cn.ffcs.util.context.ApplicationContextUtil;
public class FlowTaskCommonAction  extends FlowAppBaseAction<TransactProcessStatus>
{
    private ProcessPvcService processPvcService;
    private TaskService taskService;
    private String flowTaskId;
    private String userId;
    private CronTriggerBean cronTriggerTaskProcessMessage;
    private ProcTranRecord procTranRecord;
    private BusinessRequest businessRequest;
    private ContractRemindService contractRemindService; 
    private AppBusinessReqService appBusinessReqService;
    /**
     * 结束任务与结束需求单
     * @return
     */
    public String completeTaskAndProcessInst(){
    	try{
    		getTaskService().completeTaskAndProcess(flowTaskId, "结束需求单");
    		
    		this.returnMsg="结束需求单操作成功！";
    	}catch (Exception e) {
			// TODO: handle exception
    		this.returnMsg="结束需求单操作失败！原因："+e.getMessage();
		}
    	return SUCCESS;
    }
    /**
     * 转向传阅界面
     * TODO 请输入方法说明
     * @return
     */
    public String toTaskReadingToDo(){
        return "taskReadingToDo";
    }
    /**
     * 转向转派界面
     * TODO 请输入方法说明
     * @return
     */
    public String toTaskTransferAssign(){
        return "taskTransferAssign";
    }
    /**
     * 转向任务回退界面
     * @return
     */
    public String toTaskRollBack(){
    	ProcTranRecord taskView =getTaskService().getFlowPvcDao().getTask(Integer.valueOf(flowTaskId));
    	ApplicationContextUtil.getHttpServletRequest().setAttribute("taskView", taskView);
        return "taskRollBack";
    }
    
    /**
     * 任务转派
     * TODO 请输入方法说明
     * @return
     */
    public String commmitTaskTransferAssign(){
        String content=ApplicationContextUtil.getContextObject("commentComtent");
        userId=ApplicationContextUtil.getContextObject("userId");
        try{
        	getTaskService().taskTransferAsign(flowTaskId, content, userId);
            this.returnMsg="任务转派成功！";
        }catch (Exception e) {
			// TODO: handle exception
        	this.returnMsg=e.getMessage();
		}
        return SUCCESS;
    }
    /**
     * 任务传阅
     * TODO 请输入方法说明
     * @return
     */
    public String commitTaskReadingToDo(){
        String content=ApplicationContextUtil.getContextObject("commentComtent");
        String taskAppExt=ApplicationContextUtil.getContextObject("taskAppExt");
        String userIds[]=ApplicationContextUtil.getHttpServletRequest().getParameterValues("userId");
        Map<String,String> map=new HashMap<String,String>();
        //排除重复的人员
        for (int i = 0; i < userIds.length; i++) {
        	map.put(userIds[i], userIds[i]);
		}
        String[] fuserIds=new String[map.size()];
        map.keySet().toArray(fuserIds);
        getTaskService().taskReadingToDo(flowTaskId, content, fuserIds);
        this.returnMsg="任务传阅操作成功！";
        return SUCCESS;
    }
    /**
     * 提交任务
     * TODO 请输入方法说明
     * @return
     */
    public String commitRedoTask(){
    	String content=ApplicationContextUtil.getContextObject("commentComtent");
    	if(StringUtils.isBlank(content)){
    		content="已阅";
    	}
    	try{
    		getTaskService().completeTask(flowTaskId, content, null);
            this.returnMsg="操作成功！";
    	}catch (Exception e) {
			// TODO: handle exception
    		this.returnMsg=e.getMessage();
		}
        return SUCCESS;
    }

    /**
     * 提交任务
     * TODO 请输入方法说明
     * @return
     */
    public String commitTask(){
        String isCommitS=ApplicationContextUtil.getContextObject("isCommit");
        boolean isCommit=Boolean.valueOf(isCommitS);//保存
        HttpServletRequest req=ApplicationContextUtil.getHttpServletRequest();
        List<TaskAssignBean> nextTasksList = getNextTaskUserList(req);
        String content=req.getParameter("commentComtent");
        setCommitTaskMsg(isCommit);
        ProcTranRecord procTranRecord=null;
        if(isCommit){
            procTranRecord=getTaskService().completeTask(flowTaskId, content, nextTasksList);
            this.returnMsg="待办单提交成功！";
        }else{
            procTranRecord=getTaskService().saveTaskAssignUser(flowTaskId, content, nextTasksList);
            this.returnMsg="待办单保存成功！";
        }
        
        return SUCCESS;
    }
   
    /**
     * 任务回退
     * @return
     */
    public String rollBackTask(){
        String content=ApplicationContextUtil.getContextObject("commentComtent");
        returnMsg="待办单回退成功";
        try{
        	getTaskService().rollBackTask(flowTaskId, content);
        }catch (Exception e) {
			// TODO: handle exception
        	returnMsg=e.getMessage();
		}
        return SUCCESS;
    }
    /**
     * 
     * @return
     */
    public String printGraph(){
    	String processId=ApplicationContextUtil.getContextObject("processId");
    	HttpServletResponse response=ApplicationContextUtil.getHttpServletResponse();
    	response.setContentType("image/png");
    	FlowPvcGraphService graphService=new FlowPvcGraphService();
    	try {
			graphService.printGraph(processId, getTaskService().getFlowPvcDao(), response.getOutputStream(), null);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    
    public String printInstGraph(){
    	return "toFlowGraph";
    }
    public String printInstGraphImg(){
    	String procInstId=ApplicationContextUtil.getContextObject("procinstId");
    	if(StringUtils.isBlank(procInstId)){
    		returnMsg="对不起，流程实例ID为空，查看轨迹失败！！";
    		return SUCCESS;
    	}
    	String processId=getProcessPvcService().getFlowPvcDao().getFlowProcessId(procInstId);
    	List<Node> runNodeList=getProcessPvcService().getFlowPvcDao().findRunNodeList(procInstId);
    	Map<String, Node> runNodeMap=null;
    	if(runNodeList!=null&&runNodeList.size()>0){
    		runNodeMap=new HashMap<String, Node>();
    		for (Node node : runNodeList) {
    			runNodeMap.put(node.getId(), node);
			}
    	}
    	HttpServletResponse response=ApplicationContextUtil.getHttpServletResponse();
    	response.setContentType("image/png");

    	FlowPvcGraphService graphService=new FlowPvcGraphService();
    	try {
			graphService.printGraph(processId, getTaskService().getFlowPvcDao(), response.getOutputStream(), runNodeMap);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    
    public String findDocTemplate(){
    	return null;
    }
    public String toTaskAssign(){
    	procTranRecord=getTaskService().getFlowPvcDao().getTask(Integer.valueOf(flowTaskId));
    	
    	return "toTaskAssign";
    }
 
    public String saveTaskAssign(){
    	userId=ApplicationContextUtil.getContextObject("userId");
    	getTaskService().assignTaskToUser(flowTaskId, userId);
    	this.returnMsg="任务调整分配操作成功！！";
    	return SUCCESS;
    }
    
    public String transferProdData(){
    	//getAppBusinessReqService().getAppBusinessReqDao().tranferData();
    	//getAppBusinessReqService().getAppBusinessReqDao().tranferDataRefuse();
    	return null;
    }
    
    public String runCondRepeatRemind(){
    	getContractRemindService().runCondRepeatRemind();
    	this.returnMsg="特殊条款约束提醒操作成功！！";
    	return SUCCESS;
    }
    
    public String runContractDue2Month(){
    	getContractRemindService().runContractDue2Month();
    	this.returnMsg="到期三个月提醒操作成功！！";
    	return SUCCESS;
    }
    
    public ProcessPvcService getProcessPvcService()
    {
        return processPvcService;
    }
    public void setProcessPvcService(ProcessPvcService processPvcService)
    {
        this.processPvcService = processPvcService;
    }
    public TaskService getTaskService()
    {
        return taskService;
    }
    public void setTaskService(TaskService taskService)
    {
        this.taskService = taskService;
    }
    public String getFlowTaskId()
    {
        return flowTaskId;
    }
    public void setFlowTaskId(String flowTaskId)
    {
        this.flowTaskId = flowTaskId;
    }
	public CronTriggerBean getCronTriggerTaskProcessMessage() {
		return cronTriggerTaskProcessMessage;
	}
	public void setCronTriggerTaskProcessMessage(
			CronTriggerBean cronTriggerTaskProcessMessage) {
		this.cronTriggerTaskProcessMessage = cronTriggerTaskProcessMessage;
	}
	public ProcTranRecord getProcTranRecord() {
		return procTranRecord;
	}
	public void setProcTranRecord(ProcTranRecord procTranRecord) {
		this.procTranRecord = procTranRecord;
	}
	public ContractRemindService getContractRemindService() {
		return contractRemindService;
	}
	public void setContractRemindService(ContractRemindService contractRemindService) {
		this.contractRemindService = contractRemindService;
	}
	public BusinessRequest getBusinessRequest() {
		return businessRequest;
	}
	public void setBusinessRequest(BusinessRequest businessRequest) {
		this.businessRequest = businessRequest;
	}
	public AppBusinessReqService getAppBusinessReqService() {
		return appBusinessReqService;
	}
	public void setAppBusinessReqService(AppBusinessReqService appBusinessReqService) {
		this.appBusinessReqService = appBusinessReqService;
	}

}
